package io.renren.modules.project.service.impl;

import io.renren.common.utils.*;
import io.renren.modules.base.dao.BasePhaseInfoDao;
import io.renren.modules.base.dao.BaseTaskInfoDao;
import io.renren.modules.base.dao.BaseWorkitemInfoDao;
import io.renren.modules.base.entity.BasePhaseInfoEntity;
import io.renren.modules.base.entity.BaseTaskInfoEntity;
import io.renren.modules.base.entity.BaseWorkitemInfoEntity;
import io.renren.modules.contract.dao.ContractReceiptDao;
import io.renren.modules.contract.dao.ContractReceiptInfoDao;
import io.renren.modules.contract.dao.ContractTypeDao;
import io.renren.modules.contract.entity.ContractReceiptEntity;
import io.renren.modules.contract.entity.ContractReceiptInfoEntity;
import io.renren.modules.contract.entity.ContractTypeEntity;
import io.renren.modules.contract.service.ContractReceiptService;
import io.renren.modules.contract.service.ContractTypeService;
import io.renren.modules.project.dao.*;
import io.renren.modules.project.entity.*;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.project.service.ProjectEntityService;


@Service("projectEntityService")
public class ProjectEntityServiceImpl implements ProjectEntityService {
    @Autowired
    private ProjectEntityDao projectEntityDao;
    @Autowired
    private PojectTaskDao pojectTaskDao;
    @Autowired
    private BaseTaskInfoDao baseTaskInfoDao;
    @Autowired
    private BaseWorkitemInfoDao baseWorkitemInfoDao;
    @Autowired
    private BasePhaseInfoDao basePhaseInfoDao;
    @Autowired
    private PojectWorkitemDao pojectWorkitemDao;
    @Autowired
    private ProjectLineInfoDao projectLineInfoDao;
    @Autowired
    private NotificationDao notificationDao;
    @Autowired
    private NotificationReceiverDao notificationReceiverDao;
    @Autowired
    private ProjectApplicantInfoDao projectApplicantInfoDao;
    @Autowired
    private ContractReceiptDao contractReceiptDao;//收款合同dao
    @Autowired
    private ContractTypeDao contractTypeDao;//合同类型dao
    @Autowired
    private ContractReceiptService contractReceiptService;

    @Override
    public ProjectEntityEntity queryObject(String id) {
        ProjectEntityEntity projectEntityEntity = projectEntityDao.queryObject(id);
        List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTask(projectEntityEntity.getId());
        for (int i = 0; i < pojectTaskEntityList.size(); i++) {
            if ("3".equals(pojectTaskEntityList.get(i).getTaskState())) {
                projectEntityEntity.setProjectTaskStatus("进行中");
                projectEntityEntity.setProjectTask(pojectTaskEntityList.get(i).getTaskName());
                break;
            } else {
                if (pojectTaskEntityList.get(i).getOrder() == pojectTaskEntityList.size()) {
                    projectEntityEntity.setProjectTaskStatus("已完成");
                    projectEntityEntity.setProjectTask(pojectTaskEntityList.get(i).getTaskName());
                }

            }

        }

        return projectEntityEntity;
    }

    @Override
    public int queryProjectTotalLimt(Map<String, Object> map, String userId) {
        int task = 0;
        // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
        List<String> projectList = projectEntityDao.queryManagerOnlyList(userId);
        List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
        if (ownCreateProject != null && ownCreateProject.size() > 0) {
            projectList.addAll(ownCreateProject);
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        if (projectList != null && projectList.size() > 0) {
            map.put("projectList", projectList);
            // map.put("projectStatus", "0");
            task = projectEntityDao.queryProjectTotal(map);
        }
        return task;
    }

    @Override
    public List<ProjectEntityEntity> queryProject(Map<String, Object> map, String userId) {
        List<ProjectEntityEntity> projectEntityEntityList = null;
        //获取经理负责的项目
        List<String> projectList = projectEntityDao.queryManagerOnlyList(userId);
        //获取自己创建的项目
        List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
        if (ownCreateProject != null && ownCreateProject.size() > 0) {
            projectList.addAll(ownCreateProject);
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        map.put("projectList", projectList);
        // map.put("projectStatus", "0");
        if (projectList != null && projectList.size() > 0) {
            projectEntityEntityList = projectEntityDao.queryList(map);
        }
        String[] projectIdArr = projectList.toArray(new String[projectList.size()]);
        if (projectIdArr != null && projectIdArr.length > 0) {
            List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTaskList(projectIdArr);    // 所有任务列表
            List<PojectTaskEntity> projectTaskList = new ArrayList<>();  // 目标任务列表
            HashMap<String, List<PojectTaskEntity>> projectTaskMap = new HashMap<>();    // 按项目分别存taskList
            for (int i = 0; i < pojectTaskEntityList.size(); i++) {
                if (projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()) != null) {
                    projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()).add(pojectTaskEntityList.get(i));
                } else {
                    List<PojectTaskEntity> tempList = new ArrayList<>();
                    tempList.add(pojectTaskEntityList.get(i));
                    projectTaskMap.put(pojectTaskEntityList.get(i).getProjectId(), tempList);
                }
            }
            for (List<PojectTaskEntity> value : projectTaskMap.values()) {
                System.out.println("Value = " + value);
                for (int i = 0; i < value.size(); i++) {
                    if (!("1".equals(value.get(i).getTaskState()) || "4".equals(value.get(i).getTaskState()))) {
                        projectTaskList.add(value.get(i));
                    } else if ("1".equals(value.get(i).getTaskState()) && value.size() == value.get(i).getOrder()) {
                        projectTaskList.add(value.get(i));
                    }
                }
            }
            for (int k = 0; k < projectEntityEntityList.size(); k++) {
                for (int i = 0; i < projectTaskList.size(); i++) {
                    if ("0".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                        projectEntityEntityList.get(k).setProjectTaskStatus("已提交");
                        projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                        projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                    } else if ("1".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                        projectEntityEntityList.get(k).setProjectTaskStatus("");
                        projectEntityEntityList.get(k).setProjectTask("");
                        projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                    } else if ("2".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                        projectEntityEntityList.get(k).setProjectTaskStatus("已驳回");
                        projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                        projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                    } else if ("3".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                        projectEntityEntityList.get(k).setProjectTaskStatus("进行中");
                        projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                        projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                    } else if ("4".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                        projectEntityEntityList.get(k).setProjectTaskStatus("未开始");
                        projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                        projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                    }
                }
            }
        }

        return projectEntityEntityList;
    }

    @Override
    public List<ProjectEntityEntity> queryList(Map<String, Object> map, String userId, List<String> roleIdList) {
        //项目经理
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        //查看全部权限
        boolean admin = false;
        //选择的是项目状态为全部
        if (map.containsKey("projectStatus") && "11".equals(map.get("projectStatus"))) {
            map.remove("projectStatus");
        }
        //选择的项目经理为全部
        if (map.containsKey("projectManager") && "0".equals(map.get("projectManager"))) {
            map.remove("projectManager");
        }
        //选择的当前任务为全部
        if (map.containsKey("projectTask") && "0".equals(map.get("projectTask"))) {
            map.remove("projectTask");
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        //选择的项目类型为全部
        if (map.containsKey("projectType") && "0".equals(map.get("projectType"))) {
            map.remove("projectType");
        }
        //选择的项目级别为全部
        if (map.containsKey("projectLevel") && "0".equals(map.get("projectLevel"))) {
            map.remove("projectLevel");
        }
        //选择的项目申请方为全部
        if (map.containsKey("projectApplicant") && "0".equals(map.get("projectApplicant"))) {
            map.remove("projectApplicant");
        }
        //选择的项目所属区域为全部
        if (map.containsKey("projectArea") && "0".equals(map.get("projectArea"))) {
            map.remove("projectArea");
        }
        //选择的项目申报时间为空
        if (StringUtils.isEmpty(String.valueOf(map.get("projectTimeStart"))) || StringUtils.isEmpty((String) map.get("projectTimeEnd"))) {
            map.remove("projectTimeStart");
            map.remove("projectTimeEnd");
        }
        if (map.containsKey("paymentRatioStart") && "20".equals((String) map.get("paymentRatioStart")) && map.containsKey("paymentRatioEnd")  && "70".equals((String) map.get("paymentRatioEnd"))){
            map.remove("paymentRatioStart");
            map.remove("paymentRatioEnd");
        }
        if (map.containsKey("paymentRatioStart") && map.get("paymentRatioStart") != null){
            map.put("paymentRatioStart",new BigDecimal((String)map.get("paymentRatioStart")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        if (map.containsKey("paymentRatioEnd") && map.get("paymentRatioEnd") != null){
            map.put("paymentRatioEnd",new BigDecimal((String)map.get("paymentRatioEnd")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        List<ProjectEntityEntity> projectEntityEntityList = null;   // 此userId所参与的所有项目列表
        List<String> projectList = new ArrayList<>();
        List<String> projectIdList = new ArrayList<>(); //项目ID集合
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                if ("0101".equals(roleIdList.get(i))) {
                    admin = true;
                }
            }
        }
        if (map.containsKey("projectTask") && !"0".equals(map.get("projectTask"))) {
            List<String> projectIdListTask = projectEntityDao.queryProjectTask((String)map.get("projectTask"));
            if (projectIdListTask != null && projectIdListTask.size() > 0){
                for (int i = 0; i < projectIdListTask.size(); i++){
                    projectList.add(projectIdListTask.get(i));
                }
            }else {
                return null;
            }
        }
        if (admin) {
            List<String> queryArrList = projectEntityDao.queryArr();
            if (queryArrList != null && queryArrList.size()>0){
                for (int i = 0 ; i < queryArrList.size();i++){
                    projectList.add(queryArrList.get(i));
                }
            }
        } else {
            //只有里程碑里面需要查出已终止的项目
            map.put("projectMilepost", "3");
            // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
            List<String> projectManagerList = projectEntityDao.userQueryTask(userId);
            if (projectManagerList != null && projectManagerList.size()>0){
                for (int i = 0 ; i < projectManagerList.size();i++){
                    projectList.add(projectManagerList.get(i));
                }
            }
            //获取自己创建的项目
            List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
            if (ownCreateProject != null && ownCreateProject.size() > 0) {
                for (int i = 0 ; i < ownCreateProject.size();i++){
                    projectList.add(ownCreateProject.get(i));
                }
            }
        }
        if (projectList != null && projectList.size() > 0){
            projectList.stream().distinct();
        }
        if (zhuren) {
            if (map.containsKey("projectTask") && !"0".equals(map.get("projectTask"))) {
                List<String> projectIdListTask = projectEntityDao.queryProjectTask((String)map.get("projectTask"));
                System.out.println(projectIdListTask.size());
                if (projectIdListTask != null && projectIdListTask.size() > 0){
                    map.put("projectList", projectIdListTask);
                }else {
                    return null;
                }

            }
            map.put("director", "6");
            projectEntityEntityList = projectEntityDao.queryList(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.queryList(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.queryList(map);
                }
            }
        }
        if (projectEntityEntityList != null && projectEntityEntityList.size() > 0) {
            for (int i = 0; i < projectEntityEntityList.size(); i++) {
                String projectId = projectEntityEntityList.get(i).getId();
                projectIdList.add(projectId);

                if (projectEntityEntityList.get(i).getBigPaymentRatio() != null){
                    projectEntityEntityList.get(i).setPaymentRatio(BigDecimalUtils.bigToString(projectEntityEntityList.get(i).getBigPaymentRatio()));
                }
            }
        }
        if (projectIdList != null && projectIdList.size() > 0) {
            String[] projectIdArr = projectIdList.toArray(new String[projectIdList.size()]);
            if (projectIdArr != null && projectIdArr.length > 0) {
                List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTaskList1(projectIdArr);    // 所有任务列表
                List<PojectTaskEntity> projectTaskList = new ArrayList<>();  // 目标任务列表
                HashMap<String, List<PojectTaskEntity>> projectTaskMap = new HashMap<>();    // 按项目分别存taskList

                for (int i = 0; i < pojectTaskEntityList.size(); i++) {
                    if (projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()) != null) {
                        projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()).add(pojectTaskEntityList.get(i));
                    } else {
                        List<PojectTaskEntity> tempList = new ArrayList<>();
                        tempList.add(pojectTaskEntityList.get(i));
                        projectTaskMap.put(pojectTaskEntityList.get(i).getProjectId(), tempList);
                    }
                }

                for (List<PojectTaskEntity> value : projectTaskMap.values()) {
                    System.out.println("Value = " + value);
                    for (int i = 0; i < value.size(); i++) {
                        int order = value.get(i).getOrder();
                        if (!("1".equals(value.get(i).getTaskState()) || "4".equals(value.get(i).getTaskState()))) {
                            projectTaskList.add(value.get(i));
                        } else if ("1".equals(value.get(i).getTaskState()) && value.size() == value.get(i).getOrder()) {
                            projectTaskList.add(value.get(i));
                        }
                    }
                }
                for (int k = 0; k < projectEntityEntityList.size(); k++) {
                    for (int i = 0; i < projectTaskList.size(); i++) {
                        if ("0".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已提交");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("1".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("");
                            projectEntityEntityList.get(k).setProjectTask("");
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("2".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已驳回");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("3".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("进行中");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("4".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("未开始");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        }
                    }
                }
            }
        }
        return projectEntityEntityList;
    }

    @Override
    public int queryProjectTotal(Map<String, Object> map, String userId, List<String> roleIdList) {
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        //查看全部权限
        boolean admin = false;
        int task = 0;
        List<String> projectList = new ArrayList<>();
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                if ("0101".equals(roleIdList.get(i))) {
                    admin = true;
                }
            }
        }
        //选择的是项目状态为全部
        if (map.containsKey("projectStatus") && "11".equals(map.get("projectStatus"))) {
            map.remove("projectStatus");
        }
        //选择的项目经理为全部
        if (map.containsKey("projectManager") && "0".equals(map.get("projectManager"))) {
            map.remove("projectManager");
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        //选择的项目类型为全部
        if (map.containsKey("projectType") && "0".equals(map.get("projectType"))) {
            map.remove("projectType");
        }
        //选择的项目级别为全部
        if (map.containsKey("projectLevel") && "0".equals(map.get("projectLevel"))) {
            map.remove("projectLevel");
        }
        //选择的项目申请方为全部
        if (map.containsKey("projectApplicant") && "0".equals(map.get("projectApplicant"))) {
            map.remove("projectApplicant");
        }
        //选择的项目所属区域为全部
        if (map.containsKey("projectArea") && "0".equals(map.get("projectArea"))) {
            map.remove("projectArea");
        }
        if (admin) {
            List<String> queryArrList = projectEntityDao.queryArr();
            projectList.addAll(queryArrList);
        } else {
            //只有里程碑里面需要查出已终止的项目
            map.put("projectMilepost", "3");
            // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
            List<String> projectManagerList = projectEntityDao.userQueryTask(userId);
            projectList.addAll(projectManagerList);
            //获取自己创建的项目
            List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
            if (ownCreateProject != null && ownCreateProject.size() > 0) {
                projectList.addAll(ownCreateProject);
            }
        }
        if (zhuren == true) {
            map.put("director", "6");
            task = projectEntityDao.queryProjectTotal(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    task = projectEntityDao.queryProjectTotal(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    task = projectEntityDao.queryProjectTotal(map);
                }
            }
        }

        return task;
    }


    /**
     * 获取正在进行的任务所在的项目列表信息
     *
     * @param map
     * @param userId
     * @param roleIdList
     * @return
     */
    @Override
    public List<ProjectEntityEntity> queryProjectAttachDoingTaskList(Map<String, Object> map, String userId, List<String> roleIdList) {
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        List<ProjectEntityEntity> projectEntityEntityList = null;
        List<String> projectIdList = new ArrayList<>();//项目ID集合
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                //为主任
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                ;
            }
        }
        // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
        List<String> projectList = projectEntityDao.userQueryTask(userId);
        if (zhuren) {
            map.put("director", "6");
            projectEntityEntityList = projectEntityDao.queryProjectAttachDoingTaskList(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.queryProjectAttachDoingTaskList(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.queryProjectAttachDoingTaskList(map);
                }
            }
        }
        if (projectEntityEntityList != null && projectEntityEntityList.size() > 0) {
            for (int i = 0; i < projectEntityEntityList.size(); i++) {
                projectIdList.add(projectEntityEntityList.get(i).getId());
            }
        }
        if (projectIdList != null && projectIdList.size() > 0) {
            String[] projectIdArr = projectIdList.toArray(new String[projectIdList.size()]);
            if (projectIdArr != null && projectIdArr.length > 0) {
                List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTaskList(projectIdArr);
                for (int k = 0; k < projectEntityEntityList.size(); k++) {
                    for (int i = 0; i < pojectTaskEntityList.size(); i++) {
                        if ("3".equals(pojectTaskEntityList.get(i).getTaskState()) && pojectTaskEntityList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("进行中");
                            projectEntityEntityList.get(k).setProjectTask(pojectTaskEntityList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                            break;
                        } else if ("0".equals(pojectTaskEntityList.get(i).getTaskState()) && pojectTaskEntityList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已提交");
                            projectEntityEntityList.get(k).setProjectTask(pojectTaskEntityList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("2".equals(pojectTaskEntityList.get(i).getTaskState()) && pojectTaskEntityList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已驳回");
                            projectEntityEntityList.get(k).setProjectTask(pojectTaskEntityList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        }
                    }
                }
            }
        }
        return projectEntityEntityList;
    }


    @Override
    public int queryTotal(Map<String, Object> map) {
        int task = projectEntityDao.queryTotal(map);
        return task;
    }

    @Override
    public String save(ProjectEntityEntity projectEntity) {
        //返回值
        String msg = "成功";
        //项目ID
        String projectId = projectEntity.getId();
        // 项目经理
        String projectManager = projectEntity.getProjectManager();
        //模板ID
        String templateId = projectEntity.getTemplateId();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        projectEntity.setStartTime(dateString);
        projectEntity.setCreateData(dateString);
        int projectNumberCont = projectEntityDao.projectNumberCont(projectEntity.getProjectNumber());
        System.out.println("*****查看时候重复****" + projectNumberCont);
        if (projectNumberCont > 0) {
            msg = "该项目编号已存在";
            System.out.println(msg);
            return msg;
        }
        projectEntityDao.save(projectEntity);
        // 通过模板ID查询任务
        List<BaseTaskInfoEntity> baseTaskInfoEntityList = baseTaskInfoDao.queryTemplateTask(templateId);
        //模板任务的ID集合
        List<String> taskIdList = new ArrayList<>();
        //项目任务集合
        List<PojectTaskEntity> pojectTaskEntityList = new ArrayList<>();
        if (baseTaskInfoEntityList != null && baseTaskInfoEntityList.size() > 0) {
            for (int i = 0; i < baseTaskInfoEntityList.size(); i++) {
                PojectTaskEntity pojectTaskEntity = new PojectTaskEntity();
                //加入任务ID
                taskIdList.add(baseTaskInfoEntityList.get(i).getId());
                String pojectTaskId = RandomId.createId() + "";
                pojectTaskEntity.setId(pojectTaskId);
                pojectTaskEntity.setTaskId(baseTaskInfoEntityList.get(i).getId());
                pojectTaskEntity.setProjectId(projectId);
                pojectTaskEntity.setTaskName(baseTaskInfoEntityList.get(i).getTaskName());
                pojectTaskEntity.setPhaseId(baseTaskInfoEntityList.get(i).getPhaseId());
                pojectTaskEntity.setOperationType("0");
                //将“报装任务”责任人设置成项目创建人员，其他均设为项目经理
                if ("1545967300583".equals(baseTaskInfoEntityList.get(i).getId())) {
                    pojectTaskEntity.setTaskRespUser(projectEntity.getCreateUser());
                } else {
                    pojectTaskEntity.setTaskRespUser(projectManager);
                }
                pojectTaskEntity.setOrder(baseTaskInfoEntityList.get(i).getTemplateTaskOrder());
                if (pojectTaskEntity.getOrder() == 1) {
                    pojectTaskEntity.setTaskState("3");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = new Date();
                    pojectTaskEntity.setTaskFactStartDate(projectEntity.getProjectStartTime());
                    pojectTaskEntity.setOperationType("1");
                } else {
                    pojectTaskEntity.setTaskState("4");
                    pojectTaskEntity.setOperationType("0");
                }
                pojectTaskDao.save(pojectTaskEntity);
                //根据任务ID查询任务工作项
                List<BaseWorkitemInfoEntity> baseWorkitemInfoEntityList = baseWorkitemInfoDao.queryWorkitem(baseTaskInfoEntityList.get(i).getId());
                if (baseWorkitemInfoEntityList != null && baseWorkitemInfoEntityList.size() > 0) {
                    List<PojectWorkitemEntity> pojectWorkitemEntityList = new ArrayList<>();
                    for (int k = 0; k < baseWorkitemInfoEntityList.size(); k++) {
                        PojectWorkitemEntity pojectWorkitemEntity = new PojectWorkitemEntity();
                        pojectWorkitemEntity.setId(RandomId.createId() + k + "");
                        pojectWorkitemEntity.setProjectId(projectId);
                        pojectWorkitemEntity.setTaskId(pojectTaskId);
                        pojectWorkitemEntity.setWorkitemName(baseWorkitemInfoEntityList.get(k).getWorkitemName());
                        pojectWorkitemEntity.setDeliveryDesc(baseWorkitemInfoEntityList.get(k).getDeliveryGoods());
                        pojectWorkitemEntity.setOrder(baseWorkitemInfoEntityList.get(k).getOrder());
                        pojectWorkitemEntity.setOperationId("");
                        pojectWorkitemEntity.setCreationUser("");
                        pojectWorkitemEntity.setCreationTime(df.format(new Date()));
                        pojectWorkitemEntityList.add(pojectWorkitemEntity);
                    }
                    pojectWorkitemDao.insertWorkitem(pojectWorkitemEntityList);
                }
            }
        }
        return msg;
    }

    @Override
    public void taskPreservation(String projectId) {
        ProjectEntityEntity projectEntityEntity = projectEntityDao.queryProjectId(projectId);
        PojectTaskEntity pojectTaskEntity = pojectTaskDao.queryCurrentTaskById(projectId);
        //新增收款合同
        List<ContractTypeEntity> contractTypeList = contractTypeDao.selectList();
        if (contractTypeList != null && contractTypeList.size()>0){
            List<ContractReceiptEntity> contractReceiptList = new ArrayList<>();
            List<ContractReceiptInfoEntity> contractReceiptInfoEntities = new ArrayList<>();
            for (int i = 0 ; i < contractTypeList.size();i++){
                ContractReceiptEntity contractReceipt = new ContractReceiptEntity();
                contractReceipt.setId(UUID.randomUUID().toString().replaceAll("-",""));
                contractReceipt.setProjectId(projectId);
                contractReceipt.setContractTypeId(contractTypeList.get(i).getId());
                contractReceipt.setCreateTime(new Date());
                contractReceipt.setOrder(i+1);
                contractReceiptList.add(contractReceipt);

                ContractReceiptInfoEntity contractReceiptInfoEntity = new ContractReceiptInfoEntity();
                contractReceiptInfoEntity.setContractReceiptId(contractReceipt.getId());
                contractReceiptInfoEntity.setMoney(new BigDecimal(0));
                contractReceiptInfoEntity.setReceiptDate(new Date());
                contractReceiptInfoEntities.add(contractReceiptInfoEntity);
            }
            contractReceiptDao.insertBatch(contractReceiptList);
            contractReceiptDao.insertBatchReceiptInfo(contractReceiptInfoEntities);
        }
        /*
         * id  projectId  taskId  operatePerson  operateTime  notification_content
         *
         * */

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        NotificationEntity notificationEntity = new NotificationEntity();   // 系统通知
//        NotificationReceiverEntity notificationReceiverEntity = new NotificationReceiverEntity();
        notificationEntity.setId(RandomId.createId() + "1");
        notificationEntity.setProjectId(projectId);
        notificationEntity.setTaskId(pojectTaskEntity.getId());
        Date operateTime = new Date();
        String operateTimeString = formatter.format(operateTime);
        notificationEntity.setOperateTime(operateTimeString);
        notificationEntity.setNotificationContent(projectEntityEntity.getProjectName() + "的" + pojectTaskEntity.getTaskName() + "已开始");
        /*
         * id  notification_id  notification_receiver  receive_time  notification_status
         *
         * */

        NotificationReceiverEntity pmReceiver = new NotificationReceiverEntity();
        NotificationReceiverEntity directorReceiver = new NotificationReceiverEntity();
        pmReceiver.setId(RandomId.createId() + "1");
        pmReceiver.setNotificationId(notificationEntity.getId());
        pmReceiver.setNotificationReceiver(projectEntityEntity.getProjectManager());
        pmReceiver.setNotificationStatus(0);

        directorReceiver.setId(RandomId.createId() + "2");
        directorReceiver.setNotificationId(notificationEntity.getId());
        directorReceiver.setNotificationReceiver(pojectTaskEntity.getTaskRespUser());
        directorReceiver.setNotificationStatus(0);

        List<NotificationReceiverEntity> receiverList = new ArrayList<>();
        receiverList.add(pmReceiver);
        receiverList.add(directorReceiver);

        notificationDao.save(notificationEntity);
        notificationReceiverDao.saveNotificationReceiverList(receiverList);
    }

    @Override
    public ProjectEntityEntity queryInfoObject(String id) {
        ProjectEntityEntity projectEntityEntity = projectEntityDao.queryInfoObject(id);
        if (projectEntityEntity.getProjectApplicant() != null && !"".equals(projectEntityEntity.getProjectApplicant())) {
            String[] strs = projectEntityEntity.getProjectApplicant().split(",");
            List<String> list = Arrays.asList(strs);
            List<String> applicantInfo = projectApplicantInfoDao.queryObjectList(list);
            String s = String.join(",", applicantInfo);
            projectEntityEntity.setApplicantName(s);
        }
        if (projectEntityEntity.getRelevantLines() != null && !"".equals(projectEntityEntity.getRelevantLines())) {
            String[] strs = projectEntityEntity.getRelevantLines().split(",");
            List<String> list = Arrays.asList(strs);
            List<String> lineInfoList = projectLineInfoDao.queryObjectList(list);
            String s = String.join(",", lineInfoList);
            projectEntityEntity.setLineList(s);
        }
        return projectEntityEntity;
    }

    @Override
    public Map<String, Object> countProjectStatus(String userId, List<String> roleIdList) {
        boolean isOk = false;
        boolean director = false;
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                //为主任
                if ("6".equals(roleIdList.get(i))) {
                    director = true;
                    break;
                }
                ;
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        //先查出他负责的任务
        List<String> pojectTaskList = projectEntityDao.userQueryTask(userId);
        List<String> projectList = new ArrayList<>();
        if (pojectTaskList != null && pojectTaskList.size() > 0) {
            for (int i = 0; i < pojectTaskList.size(); i++) {
                projectList.add(pojectTaskList.get(i));
            }
        }
        if (director) {
            map.put("director", "6");
        } else {
            if (isOk) {
                //如果是项目经理，加上角色
                map.put("projectManager", userId);
                if (projectList != null && projectList.size() > 0) {
                    //如果他有负责的任务，加上他所负责的任务项目
                    map.put("projectList", projectList);
                } else {
                    map.put("projectList", projectList);
                }

            } else {
                //如果他不是项目经理，加上他所负责的任务项目
                map.put("projectManager", null);
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                } else {
                    map.put("projectList", projectList);
                }
            }
        }
        List<Map<String, Object>> mapList = projectEntityDao.countProjectStatus(map);
        Map<String, Object> mapNumber = new HashMap<>();

        if (mapList != null && mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                mapNumber.put(String.valueOf(mapList.get(i).get("projectStatus")), String.valueOf(mapList.get(i).get("conutProjectStatus")));
            }
        }
        return mapNumber;
    }

    @Override
    public Map<String, Object> projectAnalysis(String year) throws ParseException {
        List<String> projectEntityEntityList = projectEntityDao.queryYear(year);
        Map<String, Object> retMap = new HashedMap();
        if (projectEntityEntityList != null && projectEntityEntityList.size() > 0) {
            List<OverdueEntity> overdueEntitieList = projectEntityDao.queryOverdueByprojectId(projectEntityEntityList);
            //阶段集合
            List<BasePhaseInfoEntity> phaseList = basePhaseInfoDao.query();
            //统计总数变量
            int count = 0;
            //已超期
            int overdue = 0;
            //未超期
            int noOverdue = 0;
            if (phaseList != null && phaseList.size() > 0) {
                //已超期Map
                Map<String, Integer> overdueMap = new HashedMap();
                //为超期Map
                Map<String, Integer> noOverdueMap = new HashedMap();
                //阶段集合
                List<String> phase = new ArrayList<>();
                for (int i = 0; i < phaseList.size(); i++) {
                    overdueMap.put(phaseList.get(i).getPhaseName(), 0);
                    noOverdueMap.put(phaseList.get(i).getPhaseName(), 0);
                }
                if (overdueEntitieList != null && overdueEntitieList.size() > 0) {
                    for (int i = 0; i < overdueEntitieList.size(); i++) {
                        //计算已完成阶段是否超期
                        if (StringUtils.isNotEmpty(overdueEntitieList.get(i).getTaskFactStartDate()) && StringUtils.isNotEmpty(overdueEntitieList.get(i).getTaskFactEndDate())) {
                            if (StringUtils.isNotEmpty(overdueEntitieList.get(i).getDurationRequire()) && StringUtils.isNotEmpty(overdueEntitieList.get(i).getPhaseName())) {
                                if (DateUtils.stringToDate(overdueEntitieList.get(i).getTaskFactStartDate(), overdueEntitieList.get(i).getTaskFactEndDate()) > Integer.parseInt(overdueEntitieList.get(i).getDurationRequire())) {
                                    //阶段超期完成，已超期
                                    if (overdueMap.containsKey(overdueEntitieList.get(i).getPhaseName())) {
                                        overdueMap.put(overdueEntitieList.get(i).getPhaseName(), overdueMap.get(overdueEntitieList.get(i).getPhaseName()) + 1);
                                    }
                                    count++;
                                } else {
                                    //阶段为超期完成，未超期
                                    if (noOverdueMap.containsKey(overdueEntitieList.get(i).getPhaseName())) {
                                        noOverdueMap.put(overdueEntitieList.get(i).getPhaseName(), noOverdueMap.get(overdueEntitieList.get(i).getPhaseName()) + 1);
                                    }
                                }
                            }
                        }

                        //计算已开始阶段是否超期
                        if (StringUtils.isNotEmpty(overdueEntitieList.get(i).getTaskFactStartDate()) && StringUtils.isEmpty(overdueEntitieList.get(i).getTaskFactEndDate()) && !"1".equals(overdueEntitieList.get(i).getTaskFactStartDate())) {
                            if (StringUtils.isNotEmpty(overdueEntitieList.get(i).getDurationRequire()) && StringUtils.isNotEmpty(overdueEntitieList.get(i).getPhaseName())) {
                                if (DateUtils.stringToDatePast(overdueEntitieList.get(i).getTaskFactStartDate()) > Integer.parseInt(overdueEntitieList.get(i).getDurationRequire())) {
                                    //阶段超期未完成，已超期
                                    if (overdueMap.containsKey(overdueEntitieList.get(i).getPhaseName())) {
                                        overdueMap.put(overdueEntitieList.get(i).getPhaseName(), overdueMap.get(overdueEntitieList.get(i).getPhaseName()) + 1);

                                    }
                                } else {
                                    //阶段进行中，未超期
                                    if (noOverdueMap.containsKey(overdueEntitieList.get(i).getPhaseName())) {
                                        noOverdueMap.put(overdueEntitieList.get(i).getPhaseName(), noOverdueMap.get(overdueEntitieList.get(i).getPhaseName()) + 1);
                                    }
                                }
                            }
                        }
                    }
                }
                retMap.put("overdueMap", overdueMap);
                retMap.put("noOverdueMap", noOverdueMap);
                retMap.put("phase",phaseList);
                retMap.put("projectCount",projectEntityEntityList.size());
                return retMap;
            }
        }
        return null;
    }


    @Override
    public void update(ProjectEntityEntity projectEntity) {
        projectEntityDao.update(projectEntity);
    }

    @Override
    public void delete(String id) {
        projectEntityDao.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids, String cord) {
        //"所有项目列表"菜单的删除按钮  做逻辑删除
        Map<String, Object> map = new HashMap<>();
        //标识，0进行中 3终止 4删除
        String status = null;
        if ("del".equals(cord)) {
            status = "4";
        } else if ("ter".equals(cord)) {
            status = "3";
        } else if ("rec".equals(cord)) {
            //List<PojectTaskEntity> pojectTaskList = projectEntityDao.queryProjectIdTaskState(ids);
            status = "0";
        }
        map.put("ids", ids);
        map.put("cord", status);
        projectEntityDao.updateProjectStatus(map);
    }

    //根据项目ID查询
    @Override
    public List<ProjectEnclosureEntity> queryEnclosure(Map<String, Object> map, String projectId) {
        if (!"".equals(projectId) && !"000".equals(projectId)) {
            map.put("projectId", projectId);
        }
        //查询任务工作项的附件queryEnclosureTotal
        List<ProjectEnclosureEntity> projectEnclosureList = projectEntityDao.queryEnclosure(map);
        if (projectEnclosureList != null && projectEnclosureList.size() > 0) {
            for (int i = 0; i < projectEnclosureList.size(); i++) {
                projectEnclosureList.get(i).setCreationTime(DateUtils.dataString(projectEnclosureList.get(i).getCreationTime()));
            }
        }
        return projectEnclosureList;
    }

    @Override
    public List<ProjectEnclosureEntity> queryEnclosureTotal(Map<String, Object> map, String projectId) {
        if (!"".equals(projectId) && !"000".equals(projectId)) {
            map.put("projectId", projectId);
        }
        List<ProjectEnclosureEntity> projectEnclosureList = projectEntityDao.queryEnclosureTotal(map);
        return projectEnclosureList;
    }

    @Override
    public List<ProjectEnclosureEntity> queryEnclosureList(String projectId) {
        //List<ProjectEnclosureEntity> projectEnclosureEntityList = projectEntityDao.queryEnclosureList(projectId);
        return null;
    }

    @Override
    public int receivingStatisticsTotal(Map<String, Object> map, String userId, List<String> roleIdList) {
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        //查看全部权限
        boolean admin = false;
        int task = 0;
        List<String> projectList = new ArrayList<>();
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                if ("0101".equals(roleIdList.get(i))) {
                    admin = true;
                }
            }
        }
        //选择的是项目状态为全部
        if (map.containsKey("projectStatus") && "11".equals(map.get("projectStatus"))) {
            map.remove("projectStatus");
        }
        //选择的项目经理为全部
        if (map.containsKey("projectManager") && "0".equals(map.get("projectManager"))) {
            map.remove("projectManager");
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        //选择的项目类型为全部
        if (map.containsKey("projectType") && "0".equals(map.get("projectType"))) {
            map.remove("projectType");
        }
        //选择的项目级别为全部
        if (map.containsKey("projectLevel") && "0".equals(map.get("projectLevel"))) {
            map.remove("projectLevel");
        }
        //选择的项目申请方为全部
        if (map.containsKey("projectApplicant") && "0".equals(map.get("projectApplicant"))) {
            map.remove("projectApplicant");
        }
        //选择的项目所属区域为全部
        if (map.containsKey("projectArea") && "0".equals(map.get("projectArea"))) {
            map.remove("projectArea");
        }
        if (admin) {
            List<String> queryArrList = projectEntityDao.queryArr();
            projectList.addAll(queryArrList);
        } else {
            //只有里程碑里面需要查出已终止的项目
            map.put("projectMilepost", "3");
            // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
            List<String> projectManagerList = projectEntityDao.userQueryTask(userId);
            projectList.addAll(projectManagerList);
            //获取自己创建的项目
            List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
            if (ownCreateProject != null && ownCreateProject.size() > 0) {
                projectList.addAll(ownCreateProject);
            }
        }
        if (zhuren == true) {
            map.put("director", "6");
            task = projectEntityDao.receivingStatisticsTotal(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    task = projectEntityDao.receivingStatisticsTotal(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    task = projectEntityDao.receivingStatisticsTotal(map);
                }
            }
        }

        return task;
    }

    @Override
    public List<ProjectEntityEntity> receivingStatistics(Map<String, Object> map, String userId, List<String> roleIdList) {
        //项目经理
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        //查看全部权限
        boolean admin = false;
        //选择的是项目状态为全部
        if (map.containsKey("projectStatus") && "11".equals(map.get("projectStatus"))) {
            map.remove("projectStatus");
        }
        //选择的项目经理为全部
        if (map.containsKey("projectManager") && "0".equals(map.get("projectManager"))) {
            map.remove("projectManager");
        }
        //选择的当前任务为全部
        if (map.containsKey("projectTask") && "0".equals(map.get("projectTask"))) {
            map.remove("projectTask");
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        //选择的项目类型为全部
        if (map.containsKey("projectType") && "0".equals(map.get("projectType"))) {
            map.remove("projectType");
        }
        //选择的项目级别为全部
        if (map.containsKey("projectLevel") && "0".equals(map.get("projectLevel"))) {
            map.remove("projectLevel");
        }
        //选择的项目申请方为全部
        if (map.containsKey("projectApplicant") && "0".equals(map.get("projectApplicant"))) {
            map.remove("projectApplicant");
        }
        //选择的项目所属区域为全部
        if (map.containsKey("projectArea") && "0".equals(map.get("projectArea"))) {
            map.remove("projectArea");
        }
        //选择的项目申报时间为空
        if (StringUtils.isEmpty(String.valueOf(map.get("projectTimeStart"))) || StringUtils.isEmpty((String) map.get("projectTimeEnd"))) {
            map.remove("projectTimeStart");
            map.remove("projectTimeEnd");
        }
        if (map.containsKey("paymentRatioStart") && "20".equals((String) map.get("paymentRatioStart")) && map.containsKey("paymentRatioEnd")  && "70".equals((String) map.get("paymentRatioEnd"))){
            map.remove("paymentRatioStart");
            map.remove("paymentRatioEnd");
        }
        if (map.containsKey("paymentRatioStart") && map.get("paymentRatioStart") != null){
            map.put("paymentRatioStart",new BigDecimal((String)map.get("paymentRatioStart")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        if (map.containsKey("paymentRatioEnd") && map.get("paymentRatioEnd") != null){
            map.put("paymentRatioEnd",new BigDecimal((String)map.get("paymentRatioEnd")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        List<ProjectEntityEntity> projectEntityEntityList = null;   // 此userId所参与的所有项目列表
        List<String> projectList = new ArrayList<>();
        List<String> projectIdList = new ArrayList<>(); //项目ID集合
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                if ("0101".equals(roleIdList.get(i))) {
                    admin = true;
                }
            }
        }
        if (map.containsKey("projectTask") && !"0".equals(map.get("projectTask"))) {
            List<String> projectIdListTask = projectEntityDao.queryProjectTask((String)map.get("projectTask"));
            if (projectIdListTask != null && projectIdListTask.size() > 0){
                for (int i = 0; i < projectIdListTask.size(); i++){
                    projectList.add(projectIdListTask.get(i));
                }
            }else {
                return null;
            }
        }
        if (admin) {
            List<String> queryArrList = projectEntityDao.queryArr();
            if (queryArrList != null && queryArrList.size()>0){
                for (int i = 0 ; i < queryArrList.size();i++){
                    projectList.add(queryArrList.get(i));
                }
            }
        } else {
            //只有里程碑里面需要查出已终止的项目
            map.put("projectMilepost", "3");
            // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
            List<String> projectManagerList = projectEntityDao.userQueryTask(userId);
            if (projectManagerList != null && projectManagerList.size()>0){
                for (int i = 0 ; i < projectManagerList.size();i++){
                    projectList.add(projectManagerList.get(i));
                }
            }
            //获取自己创建的项目
            List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
            if (ownCreateProject != null && ownCreateProject.size() > 0) {
                for (int i = 0 ; i < ownCreateProject.size();i++){
                    projectList.add(ownCreateProject.get(i));
                }
            }
        }
        if (projectList != null && projectList.size() > 0){
            projectList.stream().distinct();
        }
        if (zhuren) {
            if (map.containsKey("projectTask") && !"0".equals(map.get("projectTask"))) {
                List<String> projectIdListTask = projectEntityDao.queryProjectTask((String)map.get("projectTask"));
                System.out.println(projectIdListTask.size());
                if (projectIdListTask != null && projectIdListTask.size() > 0){
                    map.put("projectList", projectIdListTask);
                }else {
                    return null;
                }

            }
            map.put("director", "6");
            projectEntityEntityList = projectEntityDao.receivingStatistics(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.receivingStatistics(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectEntityEntityList = projectEntityDao.receivingStatistics(map);
                }
            }
        }
        if (projectEntityEntityList != null && projectEntityEntityList.size() > 0) {
            for (int i = 0; i < projectEntityEntityList.size(); i++) {
                String projectId = projectEntityEntityList.get(i).getId();
                projectIdList.add(projectId);

                if (projectEntityEntityList.get(i).getBigPaymentRatio() != null){
                    projectEntityEntityList.get(i).setPaymentRatio(BigDecimalUtils.bigToString(projectEntityEntityList.get(i).getBigPaymentRatio()));
                }
            }
        }
        if (projectIdList != null && projectIdList.size() > 0) {
            String[] projectIdArr = projectIdList.toArray(new String[projectIdList.size()]);
            if (projectIdArr != null && projectIdArr.length > 0) {
                List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTaskList1(projectIdArr);    // 所有任务列表
                List<PojectTaskEntity> projectTaskList = new ArrayList<>();  // 目标任务列表
                HashMap<String, List<PojectTaskEntity>> projectTaskMap = new HashMap<>();    // 按项目分别存taskList

                for (int i = 0; i < pojectTaskEntityList.size(); i++) {
                    if (projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()) != null) {
                        projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()).add(pojectTaskEntityList.get(i));
                    } else {
                        List<PojectTaskEntity> tempList = new ArrayList<>();
                        tempList.add(pojectTaskEntityList.get(i));
                        projectTaskMap.put(pojectTaskEntityList.get(i).getProjectId(), tempList);
                    }
                }

                for (List<PojectTaskEntity> value : projectTaskMap.values()) {
                    System.out.println("Value = " + value);
                    for (int i = 0; i < value.size(); i++) {
                        int order = value.get(i).getOrder();
                        if (!("1".equals(value.get(i).getTaskState()) || "4".equals(value.get(i).getTaskState()))) {
                            projectTaskList.add(value.get(i));
                        } else if ("1".equals(value.get(i).getTaskState()) && value.size() == value.get(i).getOrder()) {
                            projectTaskList.add(value.get(i));
                        }
                    }
                }
                for (int k = 0; k < projectEntityEntityList.size(); k++) {
                    //统计收款总金额
                    BigDecimal receiptMoney = contractReceiptDao.receiptMoney(projectEntityEntityList.get(k).getId());
                    if (receiptMoney != null){
                        projectEntityEntityList.get(k).setGathering(receiptMoney.toString());
                    }
                    //统计付款总金额
                    List<String> payList = contractReceiptDao.getPayList(projectEntityEntityList.get(k).getId());
                    if (payList != null && payList.size() > 0){
                        BigDecimal paymentMoney = contractReceiptDao.paymentMoney(payList);
                        if (paymentMoney != null){
                            projectEntityEntityList.get(k).setPayment(paymentMoney.toString());
                        }
                    }
                    for (int i = 0; i < projectTaskList.size(); i++) {
                        if ("0".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已提交");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("1".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("");
                            projectEntityEntityList.get(k).setProjectTask("");
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("2".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("已驳回");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("3".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("进行中");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("4".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectEntityEntityList.get(k).getId())) {
                            projectEntityEntityList.get(k).setProjectTaskStatus("未开始");
                            projectEntityEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectEntityEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        }
                    }
                }
            }
        }
        return projectEntityEntityList;
    }

    @Override
    public List<ProjectExportEntity> projectExport(Map<String, Object> map, String userId, List<String> roleIdList) {
        //项目经理
        boolean isOk = false;
        //主任
        boolean zhuren = false;
        //查看全部权限
        boolean admin = false;
        //选择的是项目状态为全部
        if (map.containsKey("projectStatus") && "11".equals(map.get("projectStatus"))) {
            map.remove("projectStatus");
        }
        //选择的项目经理为全部
        if (map.containsKey("projectManager") && "0".equals(map.get("projectManager"))) {
            map.remove("projectManager");
        }
        //选择的当前任务为全部
        if (map.containsKey("projectTask") && "0".equals(map.get("projectTask"))) {
            map.remove("projectTask");
        }
        //选择的电压类型为全部
        if (map.containsKey("voltageType") && "0".equals(map.get("voltageType"))) {
            map.remove("voltageType");
        }
        //选择的项目类型为全部
        if (map.containsKey("projectType") && "0".equals(map.get("projectType"))) {
            map.remove("projectType");
        }
        //选择的项目级别为全部
        if (map.containsKey("projectLevel") && "0".equals(map.get("projectLevel"))) {
            map.remove("projectLevel");
        }
        //选择的项目申请方为全部
        if (map.containsKey("projectApplicant") && "0".equals(map.get("projectApplicant"))) {
            map.remove("projectApplicant");
        }
        //选择的项目所属区域为全部
        if (map.containsKey("projectArea") && "0".equals(map.get("projectArea"))) {
            map.remove("projectArea");
        }
        //选择的项目申报时间为空
        if (StringUtils.isEmpty(String.valueOf(map.get("projectTimeStart"))) || StringUtils.isEmpty((String) map.get("projectTimeEnd"))) {
            map.remove("projectTimeStart");
            map.remove("projectTimeEnd");
        }
        if (map.containsKey("paymentRatioStart") && "20".equals((String) map.get("paymentRatioStart")) && map.containsKey("paymentRatioEnd")  && "70".equals((String) map.get("paymentRatioEnd"))){
            map.remove("paymentRatioStart");
            map.remove("paymentRatioEnd");
        }
        if (map.containsKey("paymentRatioStart") && map.get("paymentRatioStart") != null){
            map.put("paymentRatioStart",new BigDecimal((String)map.get("paymentRatioStart")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        if (map.containsKey("paymentRatioEnd") && map.get("paymentRatioEnd") != null){
            map.put("paymentRatioEnd",new BigDecimal((String)map.get("paymentRatioEnd")).divide(new BigDecimal(100),2,BigDecimal.ROUND_HALF_UP));
        }
        List<ProjectExportEntity> projectExportEntityList = null;   // 此userId所参与的所有项目列表
        List<String> projectList = new ArrayList<>();
        List<String> projectIdList = new ArrayList<>(); //项目ID集合
        if (roleIdList != null && roleIdList.size() > 0) {
            for (int i = 0; i < roleIdList.size(); i++) {
                if ("6".equals(roleIdList.get(i))) {
                    zhuren = true;
                }
                //为项目经理
                if ("2".equals(roleIdList.get(i))) {
                    isOk = true;
                }
                if ("0101".equals(roleIdList.get(i))) {
                    admin = true;
                }
            }
        }
        if (admin) {
            List<String> queryArrList = projectEntityDao.queryArr();
            if (queryArrList != null && queryArrList.size()>0){
                for (int i = 0 ; i < queryArrList.size();i++){
                    projectList.add(queryArrList.get(i));
                }
            }
        } else {
            //只有里程碑里面需要查出已终止的项目
            map.put("projectMilepost", "3");
            // 获取任务负责人所负责任务所在的项目(但不负责此项目)id列表
            List<String> projectManagerList = projectEntityDao.userQueryTask(userId);
            if (projectManagerList != null && projectManagerList.size()>0){
                for (int i = 0 ; i < projectManagerList.size();i++){
                    projectList.add(projectManagerList.get(i));
                }
            }
            //获取自己创建的项目
            List<String> ownCreateProject = projectEntityDao.ownCreateProject(userId);
            if (ownCreateProject != null && ownCreateProject.size() > 0) {
                for (int i = 0 ; i < ownCreateProject.size();i++){
                    projectList.add(ownCreateProject.get(i));
                }
            }
        }
        if (projectList != null && projectList.size() > 0){
            projectList.stream().distinct();
        }
        if (StringUtils.isNotBlank(map.get("ids").toString())){
            map.put("ids",Arrays.asList(map.get("ids").toString().split(",")));
        }else {
            map.remove("ids");
        }
        if (zhuren) {
            map.put("director", "6");
            projectExportEntityList = projectEntityDao.exportXls(map);
        } else {
            if (isOk) {
                // 经理所负责项目信息列表
                if (!"".equals(userId)) {
                    List<String> tempProjectList = projectEntityDao.queryManagerOnlyList(userId);
                    for (int i = 0; i < tempProjectList.size(); i++) {
                        projectList.add(tempProjectList.get(i));
                    }
                }
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectExportEntityList = projectEntityDao.exportXls(map);
                }
            } else {
                if (projectList != null && projectList.size() > 0) {
                    map.put("projectList", projectList);
                    projectExportEntityList = projectEntityDao.exportXls(map);
                }
            }
        }
        if (projectExportEntityList != null && projectExportEntityList.size() > 0) {
            for (int i = 0; i < projectExportEntityList.size(); i++) {
                String voltType = projectExportEntityList.get(i).getVoltageType();
                String projectId = projectExportEntityList.get(i).getId();
                projectIdList.add(projectId);
                if ("1".equals(voltType)) {
                    projectExportEntityList.get(i).setVoltageType("中压");
                } else if ("2".equals(voltType)){
                    projectExportEntityList.get(i).setVoltageType("高压");
                }
            }
        }
        if (projectIdList != null && projectIdList.size() > 0) {
            String[] projectIdArr = projectIdList.toArray(new String[projectIdList.size()]);
            if (projectIdArr != null && projectIdArr.length > 0) {
                List<PojectTaskEntity> pojectTaskEntityList = pojectTaskDao.queryTaskList1(projectIdArr);    // 所有任务列表
                List<PojectTaskEntity> projectTaskList = new ArrayList<>();  // 目标任务列表
                HashMap<String, List<PojectTaskEntity>> projectTaskMap = new HashMap<>();    // 按项目分别存taskList

                for (int i = 0; i < pojectTaskEntityList.size(); i++) {
                    if (projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()) != null) {
                        projectTaskMap.get(pojectTaskEntityList.get(i).getProjectId()).add(pojectTaskEntityList.get(i));
                    } else {
                        List<PojectTaskEntity> tempList = new ArrayList<>();
                        tempList.add(pojectTaskEntityList.get(i));
                        projectTaskMap.put(pojectTaskEntityList.get(i).getProjectId(), tempList);
                    }
                }

                for (List<PojectTaskEntity> value : projectTaskMap.values()) {
                    System.out.println("Value = " + value);
                    for (int i = 0; i < value.size(); i++) {
                        int order = value.get(i).getOrder();
                        if (!("1".equals(value.get(i).getTaskState()) || "4".equals(value.get(i).getTaskState()))) {
                            projectTaskList.add(value.get(i));
                        } else if ("1".equals(value.get(i).getTaskState()) && value.size() == value.get(i).getOrder()) {
                            projectTaskList.add(value.get(i));
                        }
                    }
                }
                for (int k = 0; k < projectExportEntityList.size(); k++) {
                    //统计收款总金额
                    BigDecimal receiptMoney = new BigDecimal(0.0);
                    BigDecimal paymentMoney = new BigDecimal(0.0);
                    receiptMoney = contractReceiptDao.receiptMoney(projectExportEntityList.get(k).getId());
                    if (receiptMoney != null){
                        projectExportEntityList.get(k).setGathering(receiptMoney.toString());
                    }
                    //统计付款总金额
                    List<String> payList = contractReceiptDao.getPayList(projectExportEntityList.get(k).getId());
                    if (payList != null && payList.size() > 0){
                        paymentMoney = contractReceiptDao.paymentMoney(payList);
                        if (paymentMoney != null){
                            projectExportEntityList.get(k).setPayment(paymentMoney.toString());
                        }
                    }
                    if (receiptMoney!=null&&paymentMoney!=null){
                        projectExportEntityList.get(k).setProportion(paymentMoney.divide(receiptMoney,2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    for (int i = 0; i < projectTaskList.size(); i++) {
                        if ("0".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectExportEntityList.get(k).getId())) {
                            projectExportEntityList.get(k).setProjectTaskStatus("已提交");
                            projectExportEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectExportEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("1".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectExportEntityList.get(k).getId())) {
                            projectExportEntityList.get(k).setProjectTaskStatus("");
                            projectExportEntityList.get(k).setProjectTask("");
                            projectExportEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("2".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectExportEntityList.get(k).getId())) {
                            projectExportEntityList.get(k).setProjectTaskStatus("已驳回");
                            projectExportEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectExportEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("3".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectExportEntityList.get(k).getId())) {
                            projectExportEntityList.get(k).setProjectTaskStatus("进行中");
                            projectExportEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectExportEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        } else if ("4".equals(projectTaskList.get(i).getTaskState()) && projectTaskList.get(i).getProjectId().equals(projectExportEntityList.get(k).getId())) {
                            projectExportEntityList.get(k).setProjectTaskStatus("未开始");
                            projectExportEntityList.get(k).setProjectTask(projectTaskList.get(i).getTaskName());
                            projectExportEntityList.get(k).setTaskRespUser(pojectTaskEntityList.get(i).getTaskRespUser());
                        }
                    }
                }
            }
        }
        return projectExportEntityList;
    }

}
